package com.weighttracker.android;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.bappi.custom.activities.CustomTabActivity;
import com.bappi.utils.Constants;
import com.bappi.utils.DatabaseAccessor;

public class TabsController extends CustomTabActivity {

	private static final String[] TABS = { "Tab1", "Tab2", "Tab3", "Tab4",
			"Tab5" };

	private TabHost tabs = null;

	public void changeTab(int index) {
		tabs.setCurrentTab(index);
	}

	private SharedPreferences sharedPreference;
	private OnSharedPreferenceChangeListener ospcl;
	private int caloriesMaxValue, unitMaxValue, activityMaxValue;

	@Override
	public void onCreate(Bundle icicle) {
		try {
			super.onCreate(icicle);
			setContentView(R.layout.tabs_controller);
			DatabaseAccessor.initDB(this);
			String[] TAB_TITLES = { getString(R.string.text_tab1),
					getString(R.string.text_tab2),
					getString(R.string.text_tab3),
					getString(R.string.text_tab4),
					getString(R.string.text_tab5) };
			setDefaultTab(0);
			tabs = getTabHost();
			for (int i = 0; i < TABS.length; i++) {
				TabHost.TabSpec tab = tabs.newTabSpec(TABS[i]);

				ComponentName oneActivity = new ComponentName(getApplication()
						.getPackageName(), getApplication().getPackageName()
						+ "." + TABS[i]);

				tab.setContent(new Intent().setComponent(oneActivity));
				int id = R.drawable.custom_tab_1;
				switch (i) {
				case 0:
					id = R.drawable.custom_tab_1;
					break;
				case 1:
					id = R.drawable.custom_tab_2;
					break;
				case 2:
					id = R.drawable.custom_tab_3;
					break;
				case 3:
					id = R.drawable.custom_tab_4;
					break;
				case 4:
					id = R.drawable.custom_tab_5;
					break;
				}
				tab.setIndicator(new MyTabIndicator(this, id, TAB_TITLES[i]));
				tabs.addTab(tab);
			}

			// tabs.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			// //
			// @Override
			// public void onTabChanged(String tabId) {
			// Log.i("DREG", "tabId=" + tabId);
			// }
			// });

			sharedPreference = PreferenceManager
					.getDefaultSharedPreferences(this);
			caloriesMaxValue = sharedPreference.getInt(
					Constants.KEY_FOOD_CALORIES, 0);
			unitMaxValue = sharedPreference.getInt(Constants.KEY_FOOD_UNITS, 0);
			activityMaxValue = sharedPreference.getInt(
					Constants.KEY_ACTIVITIES_WORKOUT, 0);
			if (!shouldActivateApp()) {
				ospcl = new OnSharedPreferenceChangeListener() {

					public void onSharedPreferenceChanged(
							SharedPreferences sharedPreferences, String key) {
						try {
							if (Constants.KEY_FOOD_CALORIES.equals(key)) {
								caloriesMaxValue = sharedPreference.getInt(
										Constants.KEY_FOOD_CALORIES, 0);
								runOnUiThread(new Runnable() {

									@Override
									public void run() {
										try {
											if (shouldActivateApp()) {
												enableAllTabs();
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								});
							} else if (Constants.KEY_FOOD_UNITS.equals(key)) {
								unitMaxValue = sharedPreference.getInt(
										Constants.KEY_FOOD_UNITS, 0);
								runOnUiThread(new Runnable() {

									@Override
									public void run() {
										try {
											if (shouldActivateApp()) {
												enableAllTabs();
											}
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								});
							} else if (Constants.KEY_ACTIVITIES_WORKOUT
									.equals(key)) {
								activityMaxValue = sharedPreference.getInt(
										Constants.KEY_ACTIVITIES_WORKOUT, 0);
								runOnUiThread(new Runnable() {

									@Override
									public void run() {
										try {

											if (shouldActivateApp()) {
												enableAllTabs();
											}
											// setActivityViews();
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								});
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				};
				sharedPreference
						.registerOnSharedPreferenceChangeListener(ospcl);
				tabs.getTabWidget().getChildTabViewAt(0).setEnabled(false);
				tabs.getTabWidget().getChildTabViewAt(1).setEnabled(true);
				tabs.getTabWidget().getChildTabViewAt(2).setEnabled(false);
				tabs.getTabWidget().getChildTabViewAt(3).setEnabled(false);
				tabs.getTabWidget().getChildTabViewAt(4).setEnabled(false);

				tabs.setCurrentTab(1);
			} else {
				tabs.setCurrentTab(0);
				Log.i("DREG", "SECOND");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void enableAllTabs() throws Exception {
		tabs.getTabWidget().getChildTabViewAt(0).setEnabled(true);
		tabs.getTabWidget().getChildTabViewAt(1).setEnabled(true);
		tabs.getTabWidget().getChildTabViewAt(2).setEnabled(true);
		tabs.getTabWidget().getChildTabViewAt(3).setEnabled(true);
		tabs.getTabWidget().getChildTabViewAt(4).setEnabled(true);
	}

	private boolean shouldActivateApp() {
		return ((caloriesMaxValue > 0) || (unitMaxValue > 0))
				&& (activityMaxValue > 0);
	}

	@Override
	protected void onDestroy() {
		try {
			DatabaseAccessor.closeDB();
			if (sharedPreference != null && ospcl != null) {
				sharedPreference
						.unregisterOnSharedPreferenceChangeListener(ospcl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}

	public static class MyTabIndicator extends LinearLayout {
		public MyTabIndicator(Context context, int iconId, String label) {
			super(context);
			View tab = LayoutInflater.from(context).inflate(
					R.layout.tab_indicator, this);
			ImageView iv = (ImageView) tab.findViewById(R.id.tab_image);
			iv.setImageResource(iconId);
			TextView tv = (TextView) tab.findViewById(R.id.tab_label);
			tv.setText(label);
		}
	}
}